package com.decades.application.adapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;

import com.decades.application.dto.BlogDto;
import com.decades.application.dto.SlideDto;
import com.decades.application.main.InformDetailActivity;
import com.decades.application.main.R;
import com.decades.application.main.RightMenuActivity;
import com.decades.application.main.Utils;
import com.decades.application.model.Menu;
import com.squareup.picasso.Picasso;

public class FlipViewAdapter extends BaseAdapter{
	private LayoutInflater inflater;
	//Flip Page Size
	private Activity appContext;
	private ViewHolder viewHolder;
	private GridView gridView;
	private int columnWidth,columnHeight;
	private Utils utils;
	private GridViewImageAdapter adapter;
	private AutoScrollViewPager viewPager;
	private RelativeLayout rel;
	private List<Menu> flipPageSize;
	private List<SlideDto> slideList;
	private List<BlogDto> imgList;
	public FlipViewAdapter(Activity con,List<Menu> list,List<SlideDto> slideList,List<BlogDto> imgList){
		this.appContext = con;
		this.flipPageSize = list;
		this.slideList = slideList;
		this.imgList = imgList;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return (imgList.size()/4)+1;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return imgList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(position==0){
			List<BlogDto> tempList = new ArrayList<BlogDto>();
			for(int i=0;i<3;i++){
				if(imgList!=null && imgList.size()>i){
					tempList.add(imgList.get(i));
				}
			}
			
			//
			//List for view pager
			
			if(convertView== null){
				viewHolder = new ViewHolder();
				LayoutInflater inflater = (LayoutInflater) appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				utils = new Utils(appContext);
				convertView = inflater.inflate(R.layout.flip_grid_view_0, null);
				gridView = (GridView) convertView.findViewById(R.id.grid_view);
				InitilizeGridLayout();
//				viewHolder.fiftyHeader = (TextView) convertView.findViewById(R.id.textView1);
				
				viewHolder.rightMenu = (ImageView) convertView.findViewById(R.id.imageView3);
				
				viewPager = (AutoScrollViewPager) convertView.findViewById(R.id.view_pager);
				viewPager.setLayoutParams(new LinearLayout.LayoutParams(utils.getScreenWidth(),(columnWidth*48)/100));
				viewPager.setAdapter(new ImagePagerAdapter(appContext.getApplicationContext(), slideList,utils.getScreenWidth()).setInfiniteLoop(true));
		        viewPager.setInterval(3000);
		        viewPager.startAutoScroll();
		        if(tempList !=null && tempList.size()>0){
		        	viewPager.setCurrentItem(tempList.size());
		        }		        
		        
//		        rel = (RelativeLayout) convertView.findViewById(R.id.listHorizontal);
//		        rel.getLayoutParams().height = (columnWidth*50)/100;
//		        HorizontalListView listView = (HorizontalListView) convertView.findViewById(R.id.listview);   
//		        NewsMainPageAdapter adapters= new NewsMainPageAdapter(imgList);
//		        listView.setAdapter(adapters);
		        
				adapter = new GridViewImageAdapter(appContext, tempList,(columnWidth*29)/100);
				gridView.setAdapter(adapter);
				gridView.getLayoutParams().height = (columnHeight*64)/100;
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.rightMenu.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent i = new Intent(appContext, RightMenuActivity.class);
					appContext.startActivity(i);
					appContext.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
					appContext.finish();
				}
			});
//			viewHolder.fiftyHeader.setOnClickListener(new OnClickListener() {
//				
//				@Override
//				public void onClick(View v) {
//					Intent i = new Intent(appContext, InformDetailActivity.class);
//					i.putExtra("backAnimation", "fade");
//					i.putExtra("ActionBarText", appContext.getResources().getString(R.string.head_activity_cmu));
//					i.putExtra("menu_id", "25");
//					appContext.startActivity(i);
//					appContext.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
//					appContext.finish();
//				}
//			});
			return convertView;
		}else{
			List<BlogDto> tempList = new ArrayList<BlogDto>();
			for(int i=0;i<4;i++){
				if(imgList!=null && imgList.size()>(i+((position-1)*4)+3)){
					tempList.add(imgList.get(i+((position-1)*4)+3));		
				}
			}
			if(convertView== null){
				viewHolder = new ViewHolder();
				LayoutInflater inflater = (LayoutInflater) appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				utils = new Utils(appContext);
				convertView = inflater.inflate(R.layout.flip_grid_view, null);
				gridView = (GridView) convertView.findViewById(R.id.grid_view);
				InitilizeGridLayout();
				adapter = new GridViewImageAdapter(appContext, tempList,(columnHeight*22)/100);
				gridView.setAdapter(adapter);
				gridView.getLayoutParams().height = columnHeight;
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			return convertView;
		}
		
	}
	public class ViewHolder {
		private ImageView newsImage,rightMenu;
		private TextView fiftyHeader;
		private TextView pubDate;
		private TextView countryName;
	}
	private void InitilizeGridLayout() {
		Resources r = appContext.getResources();
//		float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,AppConstant.GRID_PADDING, r.getDisplayMetrics());

		columnWidth = (int) (utils.getScreenWidth());
		columnHeight = (int) (utils.getScreenHeight());

		gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
		gridView.setColumnWidth(columnWidth);
		gridView.setStretchMode(GridView.NO_STRETCH);
//		gridView.setPadding((int) padding, (int) padding, (int) padding,(int) padding);
//		gridView.setHorizontalSpacing((int) padding);
		gridView.setVerticalSpacing(3);
		
	}
	public class AppConstant {

		// Number of columns of Grid View
		public static final int NUM_OF_COLUMNS = 1;

		// Gridview image padding
//		public static final int GRID_PADDING = 2; // in dp

		// SD card image directory
		public static final String PHOTO_ALBUM = "NAT";

		// supported file formats
		public final List<String> FILE_EXTN = Arrays.asList("jpg", "jpeg","png");
	}
	public class NewsMainPageAdapter extends BaseAdapter{
		List<BlogDto> _imagePath;
		public NewsMainPageAdapter(List<BlogDto> imagePath){
			this._imagePath = imagePath;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return _imagePath.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return _imagePath.get(position);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
//			newsDto = new NewsDto();
			if(convertView== null){
				LayoutInflater inflater = (LayoutInflater) appContext.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.viewitem, null);
				ImageView showCase = (ImageView) convertView.findViewById(R.id.image);				
				
				Picasso.with(appContext).load(_imagePath.get(position).getContent())
				.placeholder(R.drawable.loading).error(R.drawable.loading)
				.into(showCase);
			}
			
			return convertView;
		}
	}
}
